<!--
 * @Author: your name
 * @Date: 2021-06-07 17:25:13
 * @LastEditTime: 2021-06-08 14:55:30
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \jz_ui\src\pages\cms\Product.vue
-->
<template>
  <div>
    <!-- 表单 搜索 -->
    <div>
      <el-row>
        <el-col :span="20">
          <el-form size="small" :inline="true" :model="param" >
            <el-form-item>
              <el-input v-model="param.name" placeholder="请输入产品名" clearable @click="loadData"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="param.price"  placeholder="请输入产品价格" clearable></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="param.status"  placeholder="请输入产品状态" clearable></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="loadData">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="4" style="text-align:right">
          <el-button type="primary" size="small"  @click="toAddHandler">添加</el-button>
        </el-col>
      </el-row>
    </div>
    <!-- 表格 显示数据 -->
    <div>
      <el-table :data="products" size="small">
        <el-table-column prop="id" label="编号"></el-table-column>
        <el-table-column prop="name" label="产品名称"></el-table-column>
        <el-table-column prop="description" label="介绍"></el-table-column>
        <el-table-column prop="price" label="价格"></el-table-column>
        <el-table-column prop="status" label="状态"></el-table-column>
        <el-table-column prop="photo" label="图片">
            <template slot-scope="scope">
                <img :src="scope.row.photo" style="width:40px;height:40px;" />
            </template>
        </el-table-column>
        <el-table-column prop="productCategory.name" label="所属类别" ></el-table-column>
        <el-table-column label="操作" width="100" align="center">
          <template v-slot="scope">
             <el-button type="text" @click="toEditHandler(scope.row)">修改</el-button>
            <el-button type="text" @click="deleteHandler(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

     <el-dialog :title="title" :visible.sync="dialogVisible" width="50%">
      <el-form size="small" :model="form" label-width="80px">
        <el-form-item label="产品名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item label="介绍">
          <el-input type="textarea" v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="状态" >
          <el-select v-model="form.status"  >    
            <el-option label="正常"  value="正常"></el-option>
            <el-option label="下架"  value="下架"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属分类" >
          <el-select v-model="form.productCategoryId">
            <el-option 
              v-for="c in productCategories" 
              :label="c.name"
              :value="c.id"
              :key="c.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitHandler">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {get, post,del} from '@/utils/request'
export default {
  data(){
    return {
      products:[],
      param:{
        name:null,
        price:null,
        status:null
      },
      dialogVisible:false,
      title:'', 
      form:{},
      productCategories:[],
    }
  },
  created(){
    // 页面渲染出来后执行查询数据操作
    this.loadData();
    this.loadproductCategories();
  },
  methods:{
    deleteHandler(id){
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = "/product/deleteById"
        del(url,{id}).then(resp => {
          this.$message({ type:"success", message:resp.message })
          this.loadData();
        })
      })
      
    },
    loadData(){
      let url = "/product/selectCascade"
      console.log(this.param)
      get(url,this.param).then(resp => {
          console.log(resp)
        this.products = resp.data;
      })
    },

     // 3. 去添加
    toAddHandler(){
      this.form = {};
      this.title = '添加产品信息';
      this.dialogVisible = true;
    },
    loadproductCategories(){
      let url = "/productCategory/findAll"
      get(url).then(resp => {
        this.productCategories = resp.data;
      })
    },
    submitHandler(){
      let url = "/product/saveOrUpdate"
      post(url,this.form).then( resp => {
        this.dialogVisible = false;
        this.$message({type:"success",message:resp.message});
        this.loadData();
      })
    },
    toEditHandler(row){
      this.form = row;
      this.title = "修改文章信息";
      this.dialogVisible = true;
    }
  }
}
</script>